home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks96 / FlyPaper.sit / Fly Paper / FlyPaper Source / WDEF / Scrappy.cpp < prev    next >
C/C++ Source or Header  |  1996-06-22  |  7KB  |  322 lines

  1. #include <A4Stuff.h>
  2.  
  3. typedef struct {
  4.     WindowPeek        theWindow;
  5.     short            varCode;
  6.     short            whichPart;
  7. } DeviceLoopData;
  8.  
  9. #define        kDragWidth                9
  10. #define        kCloseBoxSize            5    
  11. #define        kCloseBoxYMargin        4
  12. #define        kCloseBoxXMargin        2
  13.  
  14. #define        kFrameColor                1
  15. #define        kDragBarColor            5
  16. #define        kCloseBoxColor            6
  17.  
  18. #define        kDragOnLeftVarCode        1
  19.  
  20. RGBColor        gBlackColor = { 0x0000, 0x0000, 0x0000 };
  21. AuxWinHandle    gAuxInfo;
  22. Boolean            gBlackAndWhite;
  23.  
  24. pascal void myDrawDragBarProc (short depth, short flags, GDHandle device, DeviceLoopData *data);
  25. pascal long    main (short varCode, WindowPeek theWindow, short message, long param);
  26. void        getDragRect (short varCode, WindowPeek window, Rect *r);
  27. void        getCloseRect (short varCode, WindowPeek window, Rect *r);
  28. void        calcRgns (short varCode, WindowPeek theWindow);
  29. void        drawCloseBox (short varCode, WindowPeek theWindow, Boolean invert);
  30. pascal void myDrawDragBarProc (short depth, short flags, GDHandle device, DeviceLoopData *data);
  31. void        drawDragBar (short varCode, WindowPeek theWindow, short whichPart);
  32. void        doDraw (short varCode, WindowPeek theWindow, short whichPart);
  33. short        doHit (short varCode, WindowPeek theWindow, Point where);
  34. void        makeBorder (short varCode, Rect* r);
  35.  
  36. #define    kZigSize            3
  37.  
  38. void        makeBorder (short varCode, Rect* r)
  39. {
  40.     Rect    bounds = *r;
  41.     Point    start, zigFinish, finish;
  42.     short    height, slop;
  43.     short    horizontalOffset;
  44.     
  45.     // Because I hate the way quickdraw lines hang down and to the right
  46.     --bounds.bottom;
  47.     --bounds.right;
  48.     
  49.     if (varCode & kDragOnLeftVarCode) {
  50.         finish.h = bounds.right;
  51.         finish.v = bounds.top;
  52.         MoveTo (bounds.right, bounds.top);
  53.         LineTo (bounds.left, bounds.top);
  54.         LineTo (bounds.left, bounds.bottom);
  55.         LineTo (bounds.right, bounds.bottom);
  56.         start.h = bounds.right;
  57.         start.v = bounds.bottom;
  58.         horizontalOffset = -kZigSize;
  59.     } else {
  60.         finish.h = bounds.left;
  61.         finish.v = bounds.top;
  62.         MoveTo (bounds.left, bounds.top);
  63.         LineTo (bounds.right, bounds.top);
  64.         LineTo (bounds.right, bounds.bottom);
  65.         LineTo (bounds.left, bounds.bottom);
  66.         start.h = bounds.left;
  67.         start.v = bounds.bottom;
  68.         horizontalOffset = kZigSize;
  69.     }
  70.     
  71.     height = start.v - finish.v;
  72.     slop = height % (kZigSize * 2);
  73.     
  74.     start.v -= slop / 2;
  75.     zigFinish.h = finish.h;
  76.     zigFinish.v = finish.v + (slop - (slop / 2));
  77.     LineTo (start.h, start.v);
  78.     
  79.     // ZigZag from start to finish
  80.     while (start.v > zigFinish.v) {
  81.         LineTo (start.h += horizontalOffset, start.v -= kZigSize);
  82.         horizontalOffset *= -1;
  83.     }
  84.     
  85.     LineTo (finish.h, finish.v);
  86. }
  87.  
  88.  
  89. void        getDragRect (short varCode, WindowPeek window, Rect *r)
  90. {
  91.     Rect        windowRect;
  92.     
  93.     windowRect = (*window -> contRgn) -> rgnBBox;
  94.     
  95.     r -> top = windowRect.top;
  96.     r -> bottom = windowRect.bottom;
  97.     
  98.     if (varCode & kDragOnLeftVarCode)
  99.         r -> left = windowRect.left - kDragWidth - 1;
  100.     else
  101.         r -> left = windowRect.right + 1;
  102.         
  103.     r -> right = r -> left + kDragWidth;
  104. }
  105.  
  106. void        getCloseRect (short varCode, WindowPeek window, Rect *r)
  107.  
  108. {
  109.     Rect    dragRect;
  110.     
  111.     getDragRect (varCode, window, &dragRect);
  112.         
  113.     if (window -> goAwayFlag) {
  114.         r -> top = dragRect.top + kCloseBoxYMargin;
  115.         r -> left = dragRect.left + kCloseBoxXMargin;
  116.         r -> bottom = r -> top + kCloseBoxSize;
  117.         r -> right = r -> left + kCloseBoxSize;
  118.     } else {
  119.         r -> top = 0;
  120.         r -> left = 0;
  121.         r -> bottom = 0;
  122.         r -> right = 0;
  123.     }
  124. }
  125.  
  126. void        calcRgns (short varCode, WindowPeek theWindow)
  127.  
  128. {
  129.     Rect            r;
  130.     Rect            dragRect;
  131.     RgnHandle        dragRgn;
  132.  
  133.     RgnHandle        tempRgn1 = NewRgn (), tempRgn2 = NewRgn ();
  134.  
  135.     r = theWindow -> port.portRect;
  136.     OffsetRect (&r, -theWindow -> port.portBits.bounds.left,
  137.                     -theWindow -> port.portBits.bounds.top);
  138.  
  139.     OpenRgn ();
  140.     makeBorder (varCode, &r);
  141.     CloseRgn (tempRgn1);
  142.         
  143.     InsetRect (&r, -1, -1);
  144.     OpenRgn ();
  145.     makeBorder (varCode, &r);
  146.     CloseRgn (tempRgn2);
  147.     
  148.     CopyRgn (tempRgn1, theWindow -> contRgn);
  149.     DiffRgn (tempRgn2, tempRgn1, tempRgn2);
  150.     CopyRgn (tempRgn2, theWindow -> strucRgn);
  151.  
  152.     dragRgn = NewRgn ();    
  153.     getDragRect (varCode, theWindow, &dragRect);
  154.     InsetRect (&dragRect, -1, -1);
  155.     RectRgn (dragRgn, &dragRect);
  156.     
  157.     UnionRgn (dragRgn, theWindow -> strucRgn, theWindow -> strucRgn);
  158.     UnionRgn (theWindow -> strucRgn, theWindow -> contRgn, theWindow -> strucRgn);
  159.     
  160.     
  161.     DisposeRgn (dragRgn);
  162.     DisposeRgn (tempRgn1);
  163.     DisposeRgn (tempRgn2);    
  164. }
  165.  
  166. void    drawCloseBox (short varCode, WindowPeek theWindow, Boolean invert)
  167.  
  168. {
  169.     Rect        r;
  170.     RGBColor    oldBack, c = (**(**gAuxInfo).awCTable).ctTable [kFrameColor].rgb;
  171.     RGBForeColor (&c);
  172.  
  173.     if (!theWindow -> goAwayFlag)
  174.         return;
  175.         
  176.     getCloseRect (varCode, theWindow, &r);
  177.         
  178.     FrameRect (&r);
  179.     InsetRect (&r, 1, 1);
  180.                         
  181.     if (invert)
  182.         InvertRect (&r);
  183.     else
  184.         EraseRect (&r);
  185.         
  186. }
  187.  
  188. pascal void myDrawDragBarProc (short depth, short flags, GDHandle device, DeviceLoopData *data)
  189. {
  190.     EnterCodeResource ();
  191.     
  192.     Rect            dragRect;
  193.     RGBColor        oldFore, oldBack;
  194.         
  195.     GetForeColor (&oldFore);
  196.     GetBackColor (&oldBack);
  197.     
  198.     RGBColor    c = (**(**gAuxInfo).awCTable).ctTable [kDragBarColor].rgb;
  199.     RGBBackColor (&c);
  200.     
  201.     getDragRect (data -> varCode, data -> theWindow, &dragRect);
  202.     EraseRect (&dragRect);
  203.     MoveTo (dragRect.left, dragRect.top);
  204.     drawCloseBox (data -> varCode, data -> theWindow, false);
  205.     
  206.     RGBForeColor (&oldFore);
  207.     RGBBackColor (&oldBack);
  208.     
  209.     SetA4 (oldA4);
  210. }
  211.  
  212. void    drawDragBar (short varCode, WindowPeek theWindow, short whichPart)
  213.  
  214. {
  215.     DeviceLoopData        data;
  216.     
  217.     data.theWindow = theWindow;
  218.     data.varCode = varCode;
  219.     data.whichPart = whichPart;
  220.     DeviceLoop (GetGrayRgn (), (DeviceLoopDrawingUPP) myDrawDragBarProc,
  221.             (long) &data, singleDevices);
  222. }
  223.  
  224. void    doDraw (short varCode, WindowPeek theWindow, short whichPart)
  225.  
  226. {
  227.     GrafPtr            curPort;
  228.     RgnHandle        clipSave;
  229.     RgnHandle        dragContent;
  230.     Rect            r;
  231.     
  232.     if (!theWindow -> visible)
  233.         return;
  234.  
  235.     GetAuxWin ((WindowPtr) theWindow, &gAuxInfo);
  236.     
  237.     clipSave = NewRgn ();
  238.     GetPort (&curPort);
  239.     CopyRgn (curPort -> clipRgn, clipSave);
  240.     DiffRgn (curPort -> clipRgn, theWindow -> contRgn, curPort -> clipRgn);
  241.     
  242.     dragContent = NewRgn ();
  243.     getDragRect (varCode, theWindow, &r);
  244.     RectRgn (dragContent, &r);
  245.     DiffRgn (curPort -> clipRgn, dragContent, curPort -> clipRgn);
  246.     
  247.     RGBColor        saveColor;
  248.     RGBColor        frameColor;
  249.     
  250.     frameColor = (**(**gAuxInfo).awCTable).ctTable [kFrameColor].rgb;
  251.  
  252.     GetForeColor (&saveColor);
  253.     RGBForeColor (&frameColor);
  254.     
  255.     PaintRgn (theWindow -> strucRgn);
  256.     
  257.     RGBForeColor (&saveColor);
  258.     
  259.     CopyRgn (clipSave, curPort -> clipRgn);
  260.     drawDragBar (varCode, theWindow, whichPart);
  261.     
  262.     DisposeRgn (clipSave);
  263.     DisposeRgn (dragContent);
  264. }
  265.  
  266. short    doHit (short varCode, WindowPeek theWindow, Point where)
  267.  
  268. {
  269.     Rect        r;
  270.  
  271.     
  272.     if (theWindow -> hilited) {
  273.     
  274.         getCloseRect (varCode, theWindow, &r);
  275.         if (PtInRect (where, &r))
  276.             return (wInGoAway);
  277.     }
  278.         
  279.     getDragRect (varCode, theWindow, &r);
  280.     if (PtInRect (where, &r))
  281.         return (wInDrag);
  282.         
  283.     return (wInContent);
  284. }
  285.  
  286. pascal long main (short varCode, WindowPeek theWindow, short message, long param)
  287.  
  288. {
  289.     long    oldA4 = SetCurrentA4 ();
  290.     short    retVal = 0;
  291.     
  292.     switch (message) {
  293.     
  294.     
  295.         case (wDraw) :        
  296.             if (param == wInGoAway) {
  297.                 drawCloseBox (varCode, theWindow, true);
  298.             } else
  299.                 doDraw (varCode, theWindow, param);
  300.             break;
  301.             
  302.         case (wHit) :
  303.             retVal = doHit (varCode, theWindow, *((Point*)¶m));
  304.             break;
  305.             
  306.         case (wCalcRgns) :
  307.             calcRgns (varCode, theWindow);
  308.             break;
  309.             
  310.         case (wNew) :
  311.             theWindow -> dataHandle = NewHandleClear (0);
  312.             break;
  313.             
  314.         case (wDispose) :
  315.             DisposeHandle (theWindow -> dataHandle);
  316.             break;
  317.                 
  318.     }
  319.     
  320.     SetA4 (oldA4);
  321.     return (retVal);
  322. }